package com.upc.controller;

import com.upc.pojo.Dept;
import com.upc.pojo.User;
import com.upc.service.DeptService;
import com.upc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.support.ModelAndViewContainer;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class UserController {

    @Autowired //自动装配
    private UserService userService;
    @Autowired
    private DeptService deptService;

    @RequestMapping("login")
    public ModelAndView login(User user, HttpSession session) {
        User u = userService.login(user);
        ModelAndView modelAndView = new ModelAndView();
        if (u != null) {
            //登录成功
            modelAndView.setViewName("main");
            session.setAttribute("user",u);
            List<Dept> deptList =deptService.getDeptByPage(1,6);
            session.setAttribute("deptList",deptList);
        }else{
            //登录失败
            modelAndView.setViewName("index");
            modelAndView.addObject("user",user);//数据的回显
            modelAndView.addObject("err_msg","用户名或密码错误");

        }
        return modelAndView;
    }
}